package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hjq.toast.ToastUtils;
import com.qiantu.phone.R;
import com.qiantu.phone.utils.StringUtil;
import com.lxj.xpopup.core.BottomPopupView;

public class BottomShareScopeDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private CheckBox btnPublicType;
    private CheckBox btnAssignType;
    private EditText editPhone;
    private int type;

    public BottomShareScopeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_share_scope;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        btnPublicType = findViewById(R.id.btn_public_type);
        btnAssignType = findViewById(R.id.btn_assign_type);
        editPhone = findViewById(R.id.edit_phone);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    String phone = editPhone.getText() == null ? null : editPhone.getText().toString().trim();
                    if (!StringUtil.isPhoneLegal(phone) && type == 2) {
                        ToastUtils.show(R.string.phone_error_hint);
                        return;
                    }
                    mDialogClickListener.onConfirmClick(type, phone);
                }
            }
        });
        btnPublicType.setOnClickListener(mOnClickListener);
        btnAssignType.setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_public_type:
                    if(btnPublicType.isChecked()){
                        btnAssignType.setChecked(false);
                    }else{
                        btnPublicType.setChecked(true);
                    }
                    type = 1;
                    break;
                case R.id.btn_assign_type:
                    if(btnAssignType.isChecked()){
                        btnPublicType.setChecked(false);
                    }else{
                        btnAssignType.setChecked(true);
                    }
                    type = 2;
                    break;
            }
        }
    };

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(int type, String phone);
    }
}
